﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NIEN_LUAN
{
    public partial class FormCongNoNhaCungCap : Form
    {
        private Db.DataContentDataContext db = new NIEN_LUAN.Db.DataContentDataContext();

        public FormCongNoNhaCungCap()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnChonNCC_Click(object sender, EventArgs e)
        {
            Form frmChonNCC = new FormTimKiemNhaCungCap(this);
            frmChonNCC.ShowDialog();
        }

        private void FormCongNoNhaCungCap_Load(object sender, EventArgs e)
        {
            this.dataGridViewcongno.AutoGenerateColumns = false;
            this.dataGridViewcongno.DataSource = this.bsNo;
            
            var nhacungcap = (from m in db.NHACUNGCAPs
                              select m).ToList();
            this.bsnhacungcap.DataSource = nhacungcap;
            this.comboBoxNCC.DataSource = bsnhacungcap;
            this.comboBoxNCC.DisplayMember = "MaNhaCungCap";

        }

        private void btnCongno_Click(object sender, EventArgs e)
        {
            //if (this.radioButtonNCC.Checked == false && this.radioButtonTien.Checked == true)
            //{
            //    if (this.numericUDTienNo.Value <= 0)
            //    {
            //        MessageBox.Show("Chưa chọn số tiền nợ", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //        return;
            //    }

            //    //var no = (from m in db.PHIEUNHAPs
            //    //          where ((m.TongTien.Value-m.TienDaTra.Value) <= this.numericUDTienNo.Value)
            //    //          select new
            //    //          {
            //    //              MaNhaCungCap = m.NHACUNGCAP.MaNhaCungCap,
            //    //              TenNhaCungCap = m.NHACUNGCAP.TenNhaCungCap,
            //    //              SoTienNo = (m.TongTien - m.TienDaTra)
            //    //          }).ToList();

            //    //           //}).ToList();

            //    //var no2 = (from m in no
            //    //            from x in no
            //    //          where m.MaNhaCungCap==x.MaNhaCungCap
            //    //          select new {
            //    //              MaNhaCungCap = m.MaNhaCungCap,
            //    //              TenNhaCungCap = m.TenNhaCungCap,
            //    //              SoTienNo =no.Sum(k=>k.SoTienNo)


            //    //          }).Distinct().ToList();
            //    //this.bsNo.DataSource = no2;
            //    ////this.bsNo.DataSource = no;
            //    //this.bsNo.ResetBindings(true);
            //    var no = (from m in db.PHIEUNHAPs
            //              where ((m.TongTien.Value-m.TienDaTra.Value) <= this.numericUDTienNo.Value)//(m.MaNhaCungCap == this.comboBoxNCC.Text)
            //              select new
            //              {
            //                  MaNhaCungCap = m.NHACUNGCAP.MaNhaCungCap,
            //                  TenNhaCungCap = m.NHACUNGCAP.TenNhaCungCap,
            //                  SoTienNo = m.TongTien - m.TienDaTra
            //              }).ToList();
            //    var no2 = (from m in no
            //               from x in no
            //               where ((m.MaNhaCungCap == x.MaNhaCungCap) && ((m.TongTien.Value - m.TienDaTra.Value) <= this.numericUDTienNo.Value))
            //               select new
            //               {
            //                   MaNhaCungCap = m.MaNhaCungCap,
            //                   TenNhaCungCap = m.TenNhaCungCap,
            //                   SoTienNo = no.Sum(k => k.SoTienNo)////????


            //               }).Distinct().ToList();
            //    this.bsNo.DataSource = no2;
            //    this.bsNo.ResetBindings(true);

            //}

            //if (this.radioButtonNCC.Checked == true && this.radioButtonTien.Checked == false)
            //{
                if (string.IsNullOrEmpty(this.comboBoxNCC.Text))
                {
                    MessageBox.Show("Chưa chọn nhà cung cấp", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                var no = (from m in db.PHIEUNHAPs
                          where (m.MaNhaCungCap == this.comboBoxNCC.Text)
                          select new
                          {
                              MaNhaCungCap = m.NHACUNGCAP.MaNhaCungCap,
                              TenNhaCungCap = m.NHACUNGCAP.TenNhaCungCap,
                              SoTienNo = m.TongTien - m.TienDaTra
                          }).ToList();
                var no2 = (from m in no
                            from x in no
                          where m.MaNhaCungCap==x.MaNhaCungCap
                          select new {
                              MaNhaCungCap = m.MaNhaCungCap,
                              TenNhaCungCap = m.TenNhaCungCap,
                              SoTienNo =no.Sum(k=>k.SoTienNo)////????


                          }).Distinct().ToList();
                this.bsNo.DataSource = no2;
                this.bsNo.ResetBindings(true);
            //}

            //if (this.radioButtonNCC.Checked == false && this.radioButtonTien.Checked == false)
            //{

            //    var no = (from m in db.PHIEUNHAPs
            //              //where ((m.TongTien.Value - m.TienDaTra.Value) >= this.numericUDTienNo.Value)
            //              select new
            //              {
            //                  MaNhaCungCap = m.NHACUNGCAP.MaNhaCungCap,
            //                  TenNhaCungCap = m.NHACUNGCAP.TenNhaCungCap,
            //                  SoTienNo = m.TongTien - m.TienDaTra
            //              }).ToList();
            //    this.bsNo.DataSource = no;
            //    this.bsNo.ResetBindings(true);
            //}

        }
    }
}
